<template>
  <div>
    <div class="register">
      <div class="body">
        <div class="phone">
          <p>手机号</p>
          <input type="text" placeholder="请输入手机号" v-model="register.phoneNumber" @blur="checkNumber" ref="phone"
                 onkeypress="return /[\d]/.test(String.fromCharCode(event.keyCode))">
        </div>
        <div class="password">
          <p>密码</p>
          <input type="password" placeholder="请输入密码" v-model="register.password"
                 onkeypress="return /[\w]/.test(String.fromCharCode(event.keyCode))">
        </div>
        <div class="test">
          <p>手机验证</p>
          <div class="tests">
            <input type="text" placeholder="请输入验证码" v-model="register.message"
                   onkeypress="return /[\w]/.test(String.fromCharCode(event.keyCode))">
            <span class="testp" :class="{choose:change}" @click="getTest">{{time}}</span>
          </div>
        </div>
        <div class="button">
          <button @click="submit">注册</button>
        </div>
        <div class="button">
          <button @click="deletes">清除</button>
        </div>
        <div class="skip">
          <span class="sp1">有账号？</span><span class="sp2" @click="turnLogin">马上登录</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    name: "register",
    data() {
      return {
        register: {
          phoneNumber: '',
          password: '',
          message: ''
        },
        time: "发送验证码",
        change: false
      }
    },
    methods: {
      checkNumber() {
        if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(this.register.phoneNumber))) {
          alert('号码格式错误')
        }
      },
      submit() {
        if (this.register.phoneNumber == '' || this.register.password == '' || this.register.message == '') {
          alert('手机号和密码不能为空0')
        } else {
          this.$axios.get('http://127.0.0.1:8090/register', {
            params: {
              phoneNumber: this.register.phoneNumber,
              password: this.register.password,
              message: this.register.message
            }
          }).then(res => {
            if (res.data.state) {
              alert('注册成功')
            } else {
              alert('该账号已存在')
            }
          })
        }
      },
      getTest() {
        if (!this.change) {
          if (this.register.phoneNumber == '') {
            alert('手机号码不能为空1')
            return
          }
          this.change = true;
          var i = 60;
          this.time = i + "s";
          var timer = setInterval(() => {
            if (i > 0) {
              i--;
              this.time = i + "s";
            } else {
              clearInterval(timer);
              this.change = false;
              this.time = '发送验证码'
            }
          }, 1000)
        } else {
          return
        }
      },
      turnLogin() {
        this.$router.push('/');
      },
      deletes(){
        this.$cookies.remove('token', 'ss');
      }
    }
  }
</script>

<style scoped>
  .body {
    box-sizing: border-box;
    position: absolute;
    top: 205px;
    left: 50%;
    transform: translateX(-50%);
    width: 430px;
    height: 480px;
    /*background-color: red;*/
    border-radius: 8px;
    box-shadow: 0px 0px 10px 1px rgba(240, 240, 240, 1);
    border: 1px solid rgba(240, 240, 240, 1);
  }

  .phone {
    width: 350px;
    margin: 50px 0 0 40px;
  }

  .phone p {
    color: rgba(141, 141, 141, 1);
    font-size: 14px;
    font-family: SourceHanSansSC-regular;
  }

  .phone input {
    box-sizing: border-box;
    outline: none;
    width: 100%;
    height: 50px;
    background: rgba(249, 249, 249, 1);
    border-radius: 4px;
    border: 1px solid rgb(230, 230, 230);
    padding-left: 8px;
    color: #333;
  }

  .password {
    width: 350px;
    margin: 18px 0 0 40px;
  }

  .password p {
    color: rgba(141, 141, 141, 1);
    font-size: 14px;
    font-family: SourceHanSansSC-regular;
  }

  .password input {
    box-sizing: border-box;
    outline: none;
    width: 100%;
    height: 50px;
    background: rgba(249, 249, 249, 1);
    border-radius: 4px;
    border: 1px solid rgb(230, 230, 230);
    padding-left: 8px;
    color: #333;
  }

  .button {
    width: 350px;
    height: 50px;
    margin: 20px 0 0 40px;
  }

  .button button {
    width: 350px;
    height: 50px;
    line-height: 20px;
    border-radius: 4px;
    background-color: rgba(22, 174, 156, 1);
    color: rgba(16, 16, 16, 1);
    font-size: 14px;
    text-align: center;
    box-shadow: 0px 0px 10px 1px rgba(22, 174, 156, 0.5);
    font-family: Roboto;
  }

  .skip {
    width: 350px;
    margin: 16px auto;

  }

  .sp1 {
    color: rgba(141, 141, 141, 1);
    font-size: 14px;
    text-align: left;
    font-family: SourceHanSansSC-regular;
  }

  .sp2 {
    color: #16AE9C;
    font-size: 14px;
    float: right;
    font-family: SourceHanSansSC-regular;
  }

  /*.tip{*/
  /*position: absolute;*/
  /*top: 305px;*/
  /*left: 62%;*/
  /*color: rgba(255, 72, 72, 1);*/
  /*font-size: 10px;*/
  /*text-align: left;*/
  /*font-family: SourceHanSansSC-regular;*/
  /*}*/
  .test {
    width: 350px;
    margin: 18px 0 0 40px;

  }

  .test p {
    color: rgba(141, 141, 141, 1);
    font-size: 14px;
    font-family: SourceHanSansSC-regular;
  }
  .tests{
    display: flex;
    width: 100%;
    height: 50px;
  }
  .tests input {
    display: inline-block;
    width: 260px;
    height: 50px;
    box-sizing: border-box;
    outline: none;
    background: rgba(249, 249, 249, 1);
    border-radius: 4px;
    border: 1px solid rgb(230, 230, 230);
    padding-left: 8px;
    color: #333;
  }

  .testp {
    display: inline-block;
    width: 90px;
    height: 40px;
    margin: 5px;
    line-height: 40px;
    border-radius: 2px;
    background-color: #16AE9C;
    text-align: center;
  }

  .choose {
    background-color: #E5E5E6;
  }
</style>
